<template>
  <div class="component-upload-image">
    <el-upload
      action="#"
      list-type="picture-card"
      :http-request="httpRequest"
      :before-upload="beforeAvatarUpload"
      name="file"
      :show-file-list="false"
      :disabled="disabled"
      style="display: inline-block; vertical-align: top; overflow: hidden"
    >
      <el-image v-if="!value" :src="value">
        <div slot="error" class="image-slot">
          <i class="el-icon-plus" />
        </div>
      </el-image>
      <div v-else class="image">
        <el-image :src="value" :style="`width:100%;height:100%;`" fit="fill" />
        <div class="mask">
          <div class="actions">
            <span title="预览" @click.stop="dialogVisible = true">
              <i class="el-icon-zoom-in" />
            </span>
            <span v-if="!disabled" title="移除" @click.stop="removeImage">
              <i class="el-icon-delete" />
            </span>
          </div>
        </div>
      </div>
      <div slot="tip" class="el-upload__tip">{{ tipmsg }}</div>
      <!-- <div slot="tip" class="el-upload__tip" >支持上传jpg/png文件，且不超过2MB</div> -->
    </el-upload>
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <img
        :src="value"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script>
import { uploadFlie } from "@/api/common";

export default {
  data() {
    return {
      dialogVisible: false,
      imageUrl: "",
    };
  },
  mounted() {
    console.log(this.tipmsg, "---");
  },
  props: {
    value: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    tipmsg: {
      type: String,
    },
    theType: {
      type: Number,
    },
  },
  methods: {
    removeImage() {
      this.$emit("input", "");
    },
    beforeAvatarUpload(file) {
      const isImg = file.size / 1024 / 1024 < 2;
      if (!isImg) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }

      const isType = file.type === "image/png";
      const isType2 = file.type === "image/jpeg";

      if (!isType && !isType2) {
        this.$message.error("上传头像图片格式为png或jpg");
      }
      return (isType || isType2) && isImg;
    },
    httpRequest(data) {
      /** 文件类型
       *  1-内部员工照片
       *  2-内部员工身份证照片
       *  3-内部员工证书照片
       *  4-外委人员照片
       *  5-外委人员身份证照片
       *  6-外委人员证书照片
       *  7-讲师照片
       *  8-讲师身份证照片
       *  9-讲师证书照片
       *  10-企业营业执照
       *  11-企业logo
       */
      let fd = new FormData();
      fd.append("file", data.file);
      fd.append("theType", this.theType);
      uploadFlie(fd).then((res) => {
        console.log(res);
        this.$emit("input", res.result);
        this.$emit("after", res.result);
        this.$emit("isUploaded");
        // this.imageUrl = res.result;
      });
    },
    handleAvatarSuccess(res, file) {
      console.log("上传成功-----------------------------------");
      console.log(file);
      this.imageUrl = URL.createObjectURL(file.raw);
    },
  },
  watch: {},
};
</script>

<style scoped lang="scss">
.image {
  position: relative;
  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
  }
  &:hover .mask {
    opacity: 1;
  }
}
/* .avatar-uploader.is-disabled {
  cursor: not-allowed;
} */
</style>

<style scoped>
.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: auto;
}
.avatar-uploader >>> .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.component-upload-image >>> .el-upload--picture-card {
	overflow: hidden;
}
</style>